package org.example.interceptors;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.example.domain.Result;
import org.example.utils.JwtUtil;
import org.example.utils.ThreadLocalUtil;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import java.util.Map;

/**
 * @description:
 * @Author: whopxx
 * @CreateTime: 2025-01-06
 */
@Component
public class AdminInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //令牌验证
        String token = request.getHeader("Authorization");
        //验证token
        Map<String, Object> claims = JwtUtil.parseToken(token);
        String permission = (String) claims.get("permission");
        if( !"admin".equals(permission)){
            response.setStatus(401);
            return false;
        }
        return true;
    }
}
